package demo;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;

public class demo4 {
    public static void main(String[] args) {
        try {
            //得到DOM解析器的工厂实例
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            //从DOM工厂获得DOM解析
            DocumentBuilder db=dbf.newDocumentBuilder();
            //解析XML文档，得到一个Document对象，即DOM树
            Document doc=db.parse("src/xml/demo1.xml");
            //创建Brand节点
            Element brandElement=doc.createElement("Brand");
            brandElement.setAttribute("name","三星");
            //创建Type节点
            Element typrElement=doc.createElement("Type");
            typrElement.setAttribute("name","NoteX");
            //添加父子关系
            brandElement.appendChild(typrElement);
            Element phoneElement=(Element)doc.getElementsByTagName("PhoneInfo").item(0);
            phoneElement.appendChild(brandElement);
            //保存XML文件
            TransformerFactory transformerFactory=TransformerFactory.newInstance();
            Transformer transformer=transformerFactory.newTransformer();
            DOMSource domSource=new DOMSource(doc);
            //设置编码类型
            transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            StreamResult result=new StreamResult(new FileOutputStream("src/xml/demo1.xml"));
            //把DOM树转换为XML文件
            transformer.transform(domSource,result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
